<?php

namespace app\api\controller;

use app\admin\model\post\Comment;
use app\common\controller\Api;

use think\Config;
use think\Db;
use think\Exception;

/**
 * 圈子评论
 */
class PostComment extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $field=['content','reply_id','post_id'];


    public function create(){
        $params=$this->getParams($this->field);
        $params['uid']=$this->uid();
        if(empty($params['content'])) throw new Exception('内容不能为空');
        $post=\app\admin\model\post\Post::get(['id'=>$params['post_id']]);
        if(empty($post)) throw new Exception('帖子不存在');
        $result=\app\admin\model\post\Comment::create($params);
        $this->success('',$result);
    }


    public function list(){
        $page=$this->request->request('page',1);
        $post_id=$this->request->request('post_id');
        $list= (new Comment())->getList($post_id,$page);
        $this->success('',$list);
    }

    public function like(){
        $post_id=$this->request->post('post_id');
        if(!\app\admin\model\Post::get($post_id)) throw new Exception('圈子不存在');
        $post=Db::table('post_like');
        if(!$post->where(['uid'=>$this->uid(),'post_id'=>$post_id])->find()){
            $r=$post->insert(['uid'=>$this->uid(),'post_id'=>$post_id]);
            \app\admin\model\Post::where(['id'=>$post_id])->inc('like_num');
        }else{
            $r=$post->where(['uid'=>$this->uid(),'post_id'=>$post_id])->delete();
            \app\admin\model\Post::where(['id'=>$post_id])->dec('like_num');
        }
        $this->success('',$r ?? [0]);
    }


}
